package com.csc.banking.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csc.banking.form.TransferFundsForm;
import com.csc.banking.model.Customer;
import com.csc.banking.model.EntityObject;
import com.csc.banking.model.Profile;
import com.csc.banking.model.Transaction;
import com.csc.banking.service.Services;

@Controller
public class RoleController 
{
	@Autowired
	private Services services;

	@RequestMapping(value = "/role", method = RequestMethod.GET)
	public String getAllRole(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		List<EntityObject> profiles = services.getAll(Profile.class);
		model.addAttribute("profiles", profiles);
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) model.addAttribute("transaction", transaction);
		return "role";
	}
	
	@RequestMapping(value = "/addRole", method = RequestMethod.GET)
	public String addRole(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		String cardNumber=request.getParameter("cardNumber");
		String firsteName=request.getParameter("firstName");
		String lastName=request.getParameter("lastName");
		String phoneNumber=request.getParameter("phoneNumber");
		String email=request.getParameter("email");
		Profile profile= new Profile(cardNumber,firsteName,lastName,phoneNumber,"asd",email);
		services.create(profile);
		List<EntityObject> profiles = services.getAll(Profile.class);
		model.addAttribute("profiles", profiles);
		
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) model.addAttribute("transaction", transaction);
		return "role";
	}
	
	@RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
	public String deleteRole(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		Long profileId=Long.valueOf(request.getParameter("profileId").trim());
		Profile profile= (Profile) services.getProfile(profileId);
		services.delete(profile);
		List<EntityObject> profiles = services.getAll(Profile.class);
		model.addAttribute("profiles", profiles);	
		Transaction transaction=(Transaction)request.getSession().getAttribute("transaction");		
		if(transaction!=null) model.addAttribute("transaction", transaction);
		return "role";
	}
	
	@RequestMapping(value = "/login_", method = RequestMethod.GET)
	public String login(Model model, HttpServletRequest request,
			HttpServletResponse response) 
	{
		String userName=request.getParameter("userName").trim();
		String password=request.getParameter("password");
		Customer user= services.getCustomer(userName,password);
		if(user!=null)
		{	
			Transaction transaction= new Transaction(user,new Date(), new Date());
			request.getSession().setAttribute("transaction", transaction);
			model.addAttribute("depositAccount",services.getDepositAccount(transaction.getCustomer()));	
			services.create(transaction);
			model.addAttribute("tfForm", new TransferFundsForm());
			System.out.println("called\n\n");
			return "transferFunds";
		}
		else
			System.out.println("false\n\n\n");
		List<EntityObject> profiles = services.getAll(Profile.class);
		model.addAttribute("profiles", profiles);
		return "role";
	}
	
	
}